const { contextBridge, ipcRenderer } = require('electron')

// 通过contextBridge暴露安全的API给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 获取应用版本信息
  getVersion: () => {
    return process.versions
  },
  
  // 获取平台信息
  getPlatform: () => {
    return process.platform
  },
  
  // 发送消息到主进程
  sendMessage: (channel, data) => {
    // 白名单允许的频道
    const validChannels = ['app-version', 'app-close', 'app-minimize']
    if (validChannels.includes(channel)) {
      ipcRenderer.send(channel, data)
    }
  },
  
  // 监听主进程消息
  onMessage: (channel, callback) => {
    const validChannels = ['app-version-reply', 'app-update']
    if (validChannels.includes(channel)) {
      ipcRenderer.on(channel, callback)
    }
  },
  
  // 移除监听器
  removeListener: (channel, callback) => {
    ipcRenderer.removeListener(channel, callback)
  }
})

// 在窗口加载完成后通知主进程
window.addEventListener('DOMContentLoaded', () => {
  console.log('Electron preload script loaded')
})